//---------------------------------------------------------------------------

#include <vcl.h>
#include <jpeg.hpp>
#pragma hdrstop


#include "uFncGDI.h"
#include "Printer.h"
#include "uEtat.h"
#include "uDivers.h"
#define MODULE_TAG MODULE_TAG_FNCGDI

//---------------------------------------------------------------------------

#pragma package(smart_init)
int  __fastcall MyGetDeviceCaps( TCanvas *Canvas, int nIndex)
{
    int nReturn = 0;
    if( IsPrinting() && (Canvas == Glob->EICPrinter->Canvas) )
        nReturn = Glob->EICPrinter->GetDeviceCaps(nIndex);
    else if( Canvas)
        nReturn = ::_GetDeviceCaps(TAGL, Canvas->Handle, nIndex);
    return nReturn;
}


void __fastcall MyPolygon(
    TCanvas *Canvas,	// handle to device context
    const POINT *lpPoints,	// pointer to polygon's vertices
    int nCount 	// count of polygon's vertices
   )
{
    try{
        if( MyGetDeviceCaps( Canvas, POLYGONALCAPS) & PC_POLYGON)
        {
            TPoint *p = (TPoint *)lpPoints;
            ::_Polygon( TAGL, Canvas->Handle, p, nCount-1);
        }else
        {
            HRGN hrgn = ::_CreatePolygonRgn( TAGL, lpPoints, nCount, ALTERNATE);
            ::_PaintRgn( TAGL, Canvas->Handle, hrgn);
            ::_DeleteObject( TAGL, hrgn);
            int i = 0;
            ::_MoveToEx( TAGL, Canvas->Handle, lpPoints[i].x, lpPoints[i].y, NULL);
            i++;
            do{
                ::_LineTo( TAGL, Canvas->Handle, lpPoints[i].x, lpPoints[i].y);
            }while( ++i < nCount);
            ::_LineTo( TAGL, Canvas->Handle, lpPoints[0].x, lpPoints[0].y);
        }
    }CATCH_EXCEPTION( TAG( 0))
}


void __fastcall MyStretchDIBits(
    TCanvas *Canvas,	// handle of device context
    int XDest,	// x-coordinate of upper-left corner of dest. rect.
    int YDest,	// y-coordinate of upper-left corner of dest. rect.
    int nDestWidth,	// width of destination rectangle
    int nDestHeight,	// height of destination rectangle
    int XSrc,	// x-coordinate of upper-left corner of source rect.
    int YSrc,	// y-coordinate of upper-left corner of source rect.
    int nSrcWidth,	// width of source rectangle
    int nSrcHeight,	// height of source rectangle
    CONST VOID *lpBits,	// address of bitmap bits
    CONST BITMAPINFO *lpBitsInfo,	// address of bitmap data
    UINT iUsage,	// usage
    DWORD dwRop 	// raster operation code
   )
{
    try{
        if( MyGetDeviceCaps( Canvas, RASTERCAPS) & RC_STRETCHDIB)
        {
            ::_SetStretchBltMode( TAGL, Canvas->Handle, STRETCH_ORSCANS );
            int nRetour = ::StretchDIBits( Canvas->Handle, XDest, YDest, nDestWidth, nDestHeight,
                                           XSrc,  YSrc,  nSrcWidth,  nSrcHeight,
                                           lpBits, lpBitsInfo, iUsage, dwRop);
            if( nRetour == GDI_ERROR) goto BITBLT;
        }else
        { BITBLT :
            Graphics::TBitmap *Bmp = new Graphics::TBitmap();
            if( MyGetDeviceCaps( Canvas, RASTERCAPS) & RC_STRETCHBLT)
            {
                Bmp->Width  = nSrcWidth;
                Bmp->Height = nSrcHeight;
                ::_SetStretchBltMode( TAGL, Bmp->Canvas->Handle, STRETCH_ORSCANS );
                ::_StretchDIBits( TAGL, Bmp->Canvas->Handle, 0, 0, nSrcWidth, nSrcHeight,
                                 0, 0, nSrcWidth, nSrcHeight,
                                 lpBits, lpBitsInfo, iUsage, SRCCOPY);
                Canvas->CopyMode = dwRop;
                TRect r=TRect(XDest, YDest, XDest+nDestWidth, YDest+nDestHeight);
                Canvas->StretchDraw( r, Bmp);
            }else
            {
                Bmp->Width  = nDestWidth;
                Bmp->Height = nDestHeight;
                ::_SetStretchBltMode( TAGL, Bmp->Canvas->Handle, STRETCH_ORSCANS );
                ::_StretchDIBits( TAGL, Bmp->Canvas->Handle, 0, 0, nDestWidth, nDestHeight,
                                 0, 0, nSrcWidth, nSrcHeight,
                                 lpBits, lpBitsInfo, iUsage, SRCCOPY);
                ::_BitBlt( TAGL, Canvas->Handle, XDest, YDest, nDestWidth, nDestHeight,
                                 Bmp->Canvas->Handle, 0, 0, dwRop);
            }
            delete Bmp;
        }
    }CATCH_EXCEPTION( TAG( 1))
}

void __fastcall  DrawTransparentBitmap(HDC hdc, Graphics::TBitmap *Bmp, short xStart,
                                       short yStart, COLORREF cTransparentColor, double ResolSortieX, double ResolSortieY)
{
    COLORREF   cColor;
    BITMAP     bm;
    POINT      ptSize, ptSize2;
    HBITMAP    bmBmp = Bmp->Handle;
    HDC        hdcBmp = NULL;
    ::_GetObject(TAGL, bmBmp, sizeof(BITMAP), (LPSTR)&bm);
    ptSize.x = bm.bmWidth;            // Get width of bitmap
    ptSize.y = bm.bmHeight;           // Get height of bitmap

    ptSize2 = ptSize;
    if( ResolSortieX || ResolSortieY)
    {
        ptSize2.x = TCoordonnee::Twips2Pixels( TCoordonnee::Pixels2Twips( ptSize2.x, ResolSortieX), Glob->ResolutionX);
        ptSize2.y = TCoordonnee::Twips2Pixels( TCoordonnee::Pixels2Twips( ptSize2.y, ResolSortieY), Glob->ResolutionY);
    }

    if( cTransparentColor == 0xFFFFFFFF)
    {
        if( IsPrinting() && Glob->EICPrinter->PDF && !Glob->EICPrinter->MetaFile)
        {   //ATTENTION EN PDF CELA PERMET DE METTRE LE PDF DERRIERE TOUT CE QUI A ETE ECRIT PRECEDEMMENT
            // SUR LES CERFAS DES CADRES PEUVENT EMPECHER L'EDITION DE CE BITMAP
            Glob->EICPrinter->DrawTransparentBitmap( Bmp, xStart, yStart, ptSize2.x, ptSize2.y, cTransparentColor);
        }else
        {
            hdcBmp = ::_CreateCompatibleDC(TAGL, hdc);
            ::_SelectObject(TAGL, hdcBmp, bmBmp);   // Select the bitmap

            cColor = ::_SetBkColor(TAGL, hdcBmp, 0x00FFFFFF);//cTransparentColor);
            COLORREF cColor2 = ::_SetBkColor(TAGL, hdc, 0x00FFFFFF);//cTransparentColor);
            HGDIOBJ hOldBrush = ::_SelectObject( TAGL, hdc, GetStockObject(WHITE_BRUSH));

            ::_StretchBlt(TAGL, hdc,         xStart, yStart, ptSize2.x, ptSize2.y, hdcBmp/*hdcMem*/, 0, 0, ptSize.x, ptSize.y, SRCAND);

            ::_SetBkColor(TAGL, hdcBmp, cColor);
            ::_SetBkColor(TAGL, hdc, cColor2);
            ::_SelectObject( TAGL, hdc, hOldBrush);
        }
    }else
    {
        HBITMAP    bmNoirEtBlancInverse,    bmNoirEtBlanc,    bmMem;
        HBITMAP    bmNoirEtBlancInverseOld, bmNoirEtBlancOld, bmMemOld;
        HDC        hdcNoirEtBlancInverse,   hdcNoirEtBlanc,   hdcMem;
        cTransparentColor &= 0x00ffffff;

        hdcBmp = ::_CreateCompatibleDC(TAGL, hdc);
        ::_SelectObject(TAGL, hdcBmp, bmBmp);   // Select the bitmap

        // Create some DCs to hold temporary data.
        hdcNoirEtBlancInverse   = ::_CreateCompatibleDC(TAGL, hdc);
        hdcNoirEtBlanc          = ::_CreateCompatibleDC(TAGL, hdc);
        hdcMem                  = ::_CreateCompatibleDC(TAGL, hdc);

      // Create a bitmap for each DC. DCs are required for a number of GDI functions.
        // Monochrome DCs
        bmNoirEtBlancInverse    = ::_CreateBitmap(TAGL, ptSize.x, ptSize.y, 1, 1, NULL);
        bmNoirEtBlanc           = ::_CreateBitmap(TAGL, ptSize.x, ptSize.y, 1, 1, NULL);
        // Compatibles DCs
        bmMem                   = ::_CreateCompatibleBitmap(TAGL, hdc, ptSize.x, ptSize.y);

        // Each DC must select a bitmap object to store pixel data.
        bmNoirEtBlancInverseOld = ::_SelectObject(TAGL, hdcNoirEtBlancInverse, bmNoirEtBlancInverse);
        bmNoirEtBlancOld        = ::_SelectObject(TAGL, hdcNoirEtBlanc, bmNoirEtBlanc);
        bmMemOld                = ::_SelectObject(TAGL, hdcMem, bmMem);

        // Set proper mapping mode.
        ::_SetMapMode(TAGL, hdcBmp, ::_GetMapMode(TAGL, hdc));

        // Set the background color of the source DC to the color.
        // contained in the parts of the bitmap that should be transparent
        cColor = ::_SetBkColor(TAGL, hdcBmp, cTransparentColor);

        // Create the object mask for the bitmap by performing a BitBlt
        // from the source bitmap to a monochrome bitmap.
        ::_BitBlt(TAGL, hdcNoirEtBlanc,        0, 0, ptSize.x, ptSize.y, hdcBmp,            0, 0,           SRCCOPY);

        // Set the background color of the source DC back to the original color.
        ::_SetBkColor(TAGL, hdcBmp, cColor);

        // Create the inverse of the object mask.
        ::_BitBlt(TAGL, hdcNoirEtBlancInverse, 0, 0, ptSize.x, ptSize.y, hdcNoirEtBlanc,        0, 0,           NOTSRCCOPY);

        // Copy the background of the main DC to the destination.
        RECT rect = { 0, 0, ptSize.x, ptSize.y}; // on peint le fond en blanc au cas ou le bitblt plante
        ::_FillRect(TAGL, hdcMem, &rect, (HBRUSH)::GetStockObject(WHITE_BRUSH));
        ::_BitBlt(TAGL, hdcMem,                0, 0, ptSize.x, ptSize.y, hdc,                   xStart, yStart, SRCCOPY);

        // Mask out the places where the bitmap will be placed.
        ::_BitBlt(TAGL, hdcMem,                0, 0, ptSize.x, ptSize.y, hdcNoirEtBlanc,        0, 0,           SRCAND);

        // Mask out the transparent colored pixels on the bitmap.
        ::_BitBlt(TAGL, hdcBmp,                0, 0, ptSize.x, ptSize.y, hdcNoirEtBlancInverse, 0, 0,           SRCAND);

        // XOR the bitmap with the background on the destination DC.
        ::_BitBlt(TAGL, hdcMem,                0, 0, ptSize.x, ptSize.y, hdcBmp,                0, 0,           SRCPAINT);

        // Copy the destination to the screen.
        ::_BitBlt(TAGL, hdc,         xStart, yStart, ptSize.x, ptSize.y, hdcMem,                0, 0,            SRCCOPY);

        // Delete the memory bitmaps.
        ::_DeleteObject(TAGL, ::_SelectObject(TAGL, hdcNoirEtBlancInverse,  bmNoirEtBlancInverseOld));
        ::_DeleteObject(TAGL, ::_SelectObject(TAGL, hdcNoirEtBlanc,         bmNoirEtBlancOld));
        ::_DeleteObject(TAGL, ::_SelectObject(TAGL, hdcMem,                 bmMemOld));

        // Delete the memory DCs.
        ::_DeleteDC(TAGL, hdcMem);
        ::_DeleteDC(TAGL, hdcNoirEtBlancInverse);
        ::_DeleteDC(TAGL, hdcNoirEtBlanc);
    }
    if( hdcBmp) ::_DeleteDC(TAGL, hdcBmp);

}

void __fastcall MyStretchDraw(TCanvas *Canvas, const Windows::TRect &Rect, TGraphic* Graphic)
{
    try{
        if( IsPrinting() && Glob->EICPrinter->PDF)
            Glob->EICPrinter->DrawTGraphic(Graphic, Rect);
        else
        {
            if( !IsPrinting() && MyGetDeviceCaps( Canvas, RASTERCAPS) & RC_STRETCHBLT)
            {
                Canvas->StretchDraw( Rect, Graphic);
            }else
            {
                Graphics::TBitmap *Bmp = new Graphics::TBitmap();
                TRect r(0,0,Rect.Width(), Rect.Height());
                Bmp->HandleType = bmDIB;
                Bmp->Width  = Rect.Width();
                Bmp->Height = Rect.Height();
                Bmp->Canvas->CopyMode = cmSrcCopy;
                Bmp->Canvas->StretchDraw( r, Graphic);
                ::_BitBlt( TAGL, Canvas->Handle, Rect.Left, Rect.Top, Rect.Width(), Rect.Height(),
                                 Bmp->Canvas->Handle, 0, 0, Canvas->CopyMode);
                delete Bmp;
            }
        }
    }CATCH_EXCEPTION( TAG( 2))
}

void __fastcall MyDraw(TCanvas *Canvas, int X, int Y, TGraphic* Graphic)
{
    try{
        if( IsPrinting() && Glob->EICPrinter->PDF)
             Glob->EICPrinter->DrawTGraphic(Graphic, TRect(X, Y, X + Graphic->Width, Y + Graphic->Height) );
        else
        {
            if( MyGetDeviceCaps( Canvas, RASTERCAPS) & RC_STRETCHBLT)
            {
                Canvas->Draw( X, Y, Graphic);
            }else
            {
                Graphics::TBitmap *Bmp = dynamic_cast<Graphics::TBitmap*>(Graphic);
                if( !Bmp || (Bmp->HandleType != bmDIB))
                {
                    Bmp = new Graphics::TBitmap();
                    TRect r(0,0,Graphic->Width, Graphic->Height);
                    Bmp->HandleType = bmDIB;
                    Bmp->Width  = Graphic->Width;
                    Bmp->Height = Graphic->Height;
                    Bmp->Canvas->CopyMode = cmSrcCopy;
                    Bmp->Canvas->StretchDraw( r, Graphic);
                }
                ::_BitBlt( TAGL, Canvas->Handle, X, Y, Graphic->Width, Graphic->Height,
                                 Bmp->Canvas->Handle, 0, 0, Canvas->CopyMode);
                if( Bmp !=  Graphic) delete Bmp;
            }
        }
    }CATCH_EXCEPTION( TAG( 3))
}

HDC __fastcall _CreateDC( EICTag Tag,  LPCTSTR lpszDriver, LPCTSTR lpszDevice, LPCTSTR lpszOutput, CONST DEVMODE *lpInitData)
{
    HDC Retour;
    TRACEF( Tag, "CreateDC lpszDriver[%s] lpszDevice[%s] lpszOutput[%s]", lpszDriver?lpszDriver:"", lpszDevice?lpszDevice:"", lpszOutput?lpszOutput:"");
    ChkSDK( Tag, (Retour = ::CreateDC( lpszDriver, lpszDevice, lpszOutput, lpInitData)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
HDC     __fastcall _CreateCompatibleDC( EICTag Tag, HDC hdc)
{
    HDC Retour;
    TRACEF( Tag, "CreateCompatibleDC hdc[%X]", hdc);
    ChkSDK( Tag, (Retour = ::CreateCompatibleDC( hdc)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
void __fastcall _DeleteDC( EICTag Tag, HDC hdc)
{
    TRACEF( Tag, "DeleteDC hdc[%X]", hdc);
    ChkSDK( Tag, ::DeleteDC( hdc), false);
}



int __fastcall _GetDeviceCaps( EICTag Tag, HDC hdc, int nIndex)
{
    TRACEF( Tag, "GetDeviceCaps hdc[%X] nIndex[%d]", hdc, nIndex);
    return ::GetDeviceCaps( hdc, nIndex);
}

void __fastcall _ExtTextOut( EICTag Tag, HDC hdc, int X, int Y,	UINT fuOptions,	CONST RECT *lprc, LPCTSTR lpString, UINT cbCount, CONST INT *lpDx)
{
    int nRetour = 0;
    if( lprc){
        TRACEF( Tag, "ExtTextOut hdc[%X] X[%d] Y[%d] fuOptions[%u] lprc[%d,%d,%d,%d] lpString[%s], cbCount[%d], lpDx[%p]",
                             hdc,    X,    Y,    fuOptions,
                                                                   lprc->left,lprc->top,lprc->right,lprc->bottom,
                                                                                     lpString,     cbCount,     lpDx);
    }else{
        TRACEF( Tag, "ExtTextOut hdc[%X] X[%d] Y[%d] fuOptions[%u] lprc[(null)] lpString[%s], cbCount[%d], lpDx[%p]",
                             hdc,    X,    Y,    fuOptions,                     lpString,     cbCount,     lpDx);
    }
    try{
        ChkSDK( Tag, (nRetour = ::ExtTextOut( hdc, X, Y, fuOptions, lprc, lpString, cbCount, lpDx)), false);
    }catch(...){nRetour = 0;}
    if( !nRetour)
    {
        try{
         TRACEF( Tag, "ExtTextOut provoque une exception");
         ChkSDK( Tag, (nRetour = ::ExtTextOut( hdc, X, Y, fuOptions, lprc, lpString, cbCount, NULL)), false);
        }catch(...){
            TRACEF( Tag, "ExtTextOut (lpDx=NULL) : provoque encore une exception");
        }
    }
}
void    __fastcall _TextOut( EICTag Tag, HDC hdc, int nXStart, int nYStart, LPCTSTR lpString, int cbString)
{
    TRACEF( Tag, "TextOut hdc[%X] nXStart[%d] nYStart[%d], lpString[%s] cbString[%d]", hdc, nXStart, nYStart, lpString, cbString);
    ChkSDK( Tag, ::TextOut( hdc, nXStart, nYStart, lpString, cbString), false);
}

void __fastcall _FillRgn( EICTag Tag, HDC hdc, HRGN hrgn, HBRUSH hbr)
{
    TRACEF( Tag, "FillRgn hdc[%X] hrgn[%X] hbr[%X]", hdc, hrgn, hbr);
    ChkSDK( Tag, ::FillRgn( hdc, hrgn, hbr), false);
}
void    __fastcall _Rectangle(EICTag Tag, HDC hdc, int nLeftRect, int nTopRect,	int nRightRect,	int nBottomRect)
{
    TRACEF( Tag, "Rectangle hdc[%X] nLeftRect[%d] nTopRect[%d] nRightRect[%d] nBottomRect[%d]",
                            hdc,    nLeftRect,    nTopRect,    nRightRect,    nBottomRect);
    ChkSDK( Tag, ::Rectangle( hdc,  nLeftRect,    nTopRect,    nRightRect,    nBottomRect), false);
}
void    __fastcall _RoundRect(EICTag Tag, HDC hdc, int nLeftRect, int nTopRect,	int nRightRect,	int nBottomRect, int nWidth, int nHeight)
{
    TRACEF( Tag, "RoundRect hdc[%X] nLeftRect[%d] nTopRect[%d] nRightRect[%d] nBottomRect[%d] nWidth[%d] nHeight[%d]",
                            hdc,    nLeftRect,    nTopRect,    nRightRect,    nBottomRect,    nWidth,    nHeight);
    ChkSDK( Tag, ::RoundRect( hdc,  nLeftRect,    nTopRect,    nRightRect,    nBottomRect,    nWidth,    nHeight), false);
}
void    __fastcall _Ellipse(EICTag Tag, HDC hdc, int nLeftRect, int nTopRect,	int nRightRect,	int nBottomRect)
{
    TRACEF( Tag, "Ellipse hdc[%X] nLeftRect[%d] nTopRect[%d] nRightRect[%d] nBottomRect[%d]",
                          hdc,    nLeftRect,    nTopRect,    nRightRect,    nBottomRect);
    ChkSDK( Tag, ::Ellipse( hdc,  nLeftRect,    nTopRect,    nRightRect,    nBottomRect), false);
}
void    __fastcall _Polygon(EICTag Tag, HDC hdc, CONST POINT *lpPoints, int nCount)
{
    TRACEF( Tag, "Polygon hdc[%X] lpPoints[%p] nCount[%d]", hdc, lpPoints, nCount);
    ChkSDK( Tag, ::Polygon( hdc, lpPoints, nCount), false);
}

void    __fastcall _BitBlt( EICTag Tag, HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight,
                                        HDC hdcSrc,  int nXSrc,  int nYSrc,                         DWORD dwRop)
{
    TRACEF( Tag, "BitBlt hdcDest[%X] nXDest[%d] nYDest[%d] nWidth[%d] nHeight[%d]"
                        "hdcSrc[%X]  nXSrc[%d]  nYSrc[%d] dwRop[%X]",
                         hdcDest,    nXDest,    nYDest,    nWidth,    nHeight,
                         hdcSrc,     nXSrc,     nYSrc,    dwRop);
    BOOL bBitBlt = ::BitBlt( hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop);
    if( Win32Platform == VER_PLATFORM_WIN32_NT)
        ChkSDK( Tag, bBitBlt, false);
}
void    __fastcall _StretchBlt( EICTag Tag, HDC hdcDest, int XDest, int YDest, int nDestWidth, int nDestHeight,
                                            HDC hdcSrc,  int XSrc,  int YSrc,  int nSrcWidth,  int nSrcHeight, DWORD dwRop)
{
    TRACEF( Tag, "StretchBlt hdcDest[%X] XDest[%d] YDest[%d] nDestWidth[%d] nDestHeight[%d] "
                            "hdcSrc[%X]  XSrc[%d]  YSrc[%d]  nSrcWidth[%d]  nSrcHeight[%d] dwRop[%X]",
                             hdcDest,    XDest,    YDest,    nDestWidth,    nDestHeight,
                             hdcSrc,     XSrc,     YSrc,     nSrcWidth,     nSrcHeight,    dwRop);

    BOOL Retour  = ::StretchBlt( hdcDest, XDest, YDest, nDestWidth, nDestHeight,
                           hdcSrc, XSrc,  YSrc,   nSrcWidth,  nSrcHeight, dwRop);
    if( !Retour)
    {
        ChkSDK( Tag, Retour, false);
        Graphics::TBitmap *Bmp = new Graphics::TBitmap();
        Bmp->HandleType = bmDIB;
        Bmp->Width  = nDestWidth;
        Bmp->Height = nDestHeight;
        ::_StretchBlt( TAGL, Bmp->Canvas->Handle, 0, 0, nDestWidth, nDestHeight,
                             hdcSrc,              0, 0, nSrcWidth, nSrcHeight, SRCCOPY);

        ::_BitBlt( TAGL, hdcDest,             XDest, YDest, nDestWidth, nDestHeight,
                         Bmp->Canvas->Handle, 0,     0, dwRop);
        delete Bmp;
    }
}

void     __fastcall _StretchDIBits( EICTag Tag, HDC hdc, int XDest, int YDest, int nDestWidth, int nDestHeight,
                                                        int XSrc,  int YSrc,  int nSrcWidth,  int nSrcHeight,
                                               CONST VOID *lpBits,	CONST BITMAPINFO *lpBitsInfo, UINT iUsage, DWORD dwRop)
{
    TRACEF( Tag, "StretchDIBits hdc[%X] XDest[%d] YDest[%d] nDestWidth[%d] nDestHeight[%d]"
                                       "XSrc[%d]  YSrc[%d]  nSrcWidth[%d]  nSrcHeight[%d]"
                               "lpBits[%X] lpBitsInfo[%X] iUsage[%u] dwRop[%X]",
                                hdc,    XDest,    YDest,    nDestWidth,    nDestHeight,
                                        XSrc,     YSrc,     nSrcWidth,     nSrcHeight,
                                lpBits,    lpBitsInfo,    iUsage,    dwRop);
    ChkSDK( Tag, ::StretchDIBits( hdc, XDest, YDest, nDestWidth, nDestHeight,
                                       XSrc,  YSrc,  nSrcWidth,  nSrcHeight,
                                  lpBits, lpBitsInfo, iUsage, dwRop), GDI_ERROR);
}
void    __fastcall _PaintRgn( EICTag Tag, HDC hdc, HRGN hrgn)
{
    TRACEF( Tag, "PaintRgn hdc[%X] hrgn[%X]", hdc, hrgn);
    ChkSDK( Tag, ::PaintRgn( hdc, hrgn), false);
}
void    __fastcall _FillRect( EICTag Tag, HDC hdc, CONST RECT *lprc, HBRUSH hbr)
{
    TRACEF( Tag, "FillRect hdc[%X] lprc[%d,%d,%d,%d] hbr[%X]", hdc, lprc->left, lprc->top, lprc->right, lprc->bottom, hbr);
    ChkSDK( Tag, ::FillRect( hdc, lprc, hbr), false);
}
void    __fastcall _MoveToEx( EICTag Tag, HDC hdc, int X, int Y, LPPOINT lpPoint)
{
    TRACEF( Tag, "MoveToEx hdc[%X] X[%d] Y[%d] lpPoint[%X]", hdc, X, Y, lpPoint);
    ChkSDK( Tag, ::MoveToEx( hdc, X, Y, lpPoint), false);
}
void    __fastcall _LineTo(EICTag Tag, HDC hdc,	int nXEnd, int nYEnd)
{
    TRACEF( Tag, "LineTo hdc[%X] nXEnd[%d] YDest[%d] nYEnd[%d]", hdc, nXEnd, nYEnd);
    ChkSDK( Tag, ::LineTo( hdc, nXEnd, nYEnd), false);
}
int     __fastcall _SetStretchBltMode( EICTag Tag, HDC hdc, int iStretchMode)
{
    int Retour;
    TRACEF( Tag, "SetStretchBltMode hdc[%X] iStretchMode[%d]", hdc, iStretchMode);
    ChkSDK( Tag, (Retour=::SetStretchBltMode( hdc, iStretchMode)), 0);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    return Retour;
}

void __fastcall _GetTextMetrics( EICTag Tag, HDC hdc, LPTEXTMETRIC lptm)
{
    TRACEF( Tag, "GetTextMetrics hdc[%X] lptm[%p]", hdc, lptm);
    ChkSDK( Tag, ::GetTextMetrics( hdc, lptm), ERROR);
    if(lptm)
        TRACEF( Tag, "\t\tRetour lptm.tmBreakChar['%c';%2X]", hdc, lptm->tmBreakChar, (int)lptm->tmBreakChar);
}

UINT __fastcall _GetTextAlign( EICTag Tag, HDC hdc)
{
    UINT Retour;
    TRACEF( Tag, "GetTextAlign hdc[%X]", hdc);
    ChkSDK( Tag, (Retour = ::GetTextAlign( hdc)), GDI_ERROR);
    TRACEF( Tag, "\t\tRetour[%u]", Retour);
    return Retour;
}
UINT __fastcall _SetTextAlign( EICTag Tag, HDC hdc,	UINT fMode)
{
    UINT Retour;
    TRACEF( Tag, "SetTextAlign hdc[%X] fMode[%u]", hdc, fMode);
    ChkSDK( Tag, (Retour = ::SetTextAlign( hdc, fMode)), GDI_ERROR);
    TRACEF( Tag, "\t\tRetour[%u]", Retour);
    return Retour;
}
/*int __fastcall _GetTextCharacterExtra( EICTag Tag, HDC hdc)
{
    int Retour;
    TRACEF( Tag, "GetTextCharacterExtra hdc[%X] ", hdc);
    ChkSDK( Tag, (Retour =::GetTextCharacterExtra( hdc)), 0x80000000);
    return Retour;
}
int __fastcall _SetTextCharacterExtra( EICTag Tag, HDC hdc, int nCharExtra)
{
    int Retour;
    TRACEF( Tag, "SetTextCharacterExtra hdc[%X] nCharExtra[%d]", hdc, nCharExtra);
    ChkSDK( Tag, (Retour =::SetTextCharacterExtra( hdc, nCharExtra)), 0x80000000);
    return Retour;
}
void __fastcall _SetTextJustification(EICTag Tag, HDC hdc, int nBreakExtra, int nBreakCount)
{
    TRACEF( Tag, "SetTextJustification hdc[%X] nBreakExtra[%d] nBreakCount[%d]", hdc, nBreakExtra, nBreakCount);
    if( nBreakCount)
        ChkSDK( Tag, ::SetTextJustification( hdc, nBreakExtra, nBreakCount), false);
}*/
void __fastcall _GetTextExtentPoint32( EICTag Tag, HDC hdc, LPCTSTR lpString, int cbString, LPSIZE lpSize)
{
    TRACEF( Tag, "GetTextExtentPoint32 hdc[%X] lpString[%s] cbString[%d] lpSize[%p]", hdc, lpString, cbString, lpSize);
    ChkSDK( Tag, ::GetTextExtentPoint32( hdc, lpString, cbString, lpSize), false);
    TRACEF( Tag, "\t\tRetour Size:[%d;%d]", lpSize->cx, lpSize->cy);
}
void __fastcall _GetTextExtentExPoint( EICTag Tag, HDC hdc, LPCTSTR lpszStr,	int cchString, int nMaxExtent, LPINT lpnFit, LPINT alpDx, LPSIZE lpSize)
{
    TRACEF( Tag, "GetTextExtentExPoint hdc[%X] lpszStr[%s] cchString[%d] nMaxExtent[%d] lpnFit[%p] alpDx[%p] lpSize[%p]",
                                       hdc,    lpszStr,    cchString,    nMaxExtent,    lpnFit,    alpDx,    lpSize);
    ChkSDK( Tag, ::GetTextExtentExPoint( hdc,  lpszStr,    cchString, nMaxExtent, lpnFit, alpDx, lpSize), false);
    TRACEF( Tag, "\t\tRetour Size:[%d;%d]", lpSize->cx, lpSize->cy);
}
//  #define NOCLIP
int __fastcall _GetClipRgn( EICTag Tag, HDC hdc, HRGN hrgn)
{
//    if( IsPrinting()) return 0;
    int Retour;
    TRACEF( Tag, "GetClipRgn hdc[%X] hrgn[%X]", hdc, hrgn);
    ChkSDK( Tag, (Retour =::GetClipRgn( hdc, hrgn)), -1);
    TRACEF( Tag, "\t\tRetour[%u]", Retour);
    return Retour;
}
int __fastcall _GetClipBox( EICTag Tag, HDC hdc, LPRECT lprc)
{
//    if( IsPrinting()) return SIMPLEREGION;
    int Retour;
    TRACEF( Tag, "GetClipBox hdc[%X] lprc[%p]", hdc, lprc);
    ChkSDK( Tag, (Retour =::GetClipBox( hdc, lprc)), ERROR);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    return Retour;
}

int __fastcall _SelectClipRgn( EICTag Tag, HDC hdc,	HRGN hrgn)
{
//    if( IsPrinting()) return SIMPLEREGION;
    LPRGNDATA lpRgnData = NULL;
    if( IsPrinting() && Glob->EICPrinter->PDF && hrgn)
    {
      XFORM Xform;
      memset( &Xform, 0, sizeof( Xform));
      Xform.eM11 = (Glob->EICPrinter->ResolPourRTF / Glob->ResolutionX);
      Xform.eM22 = (Glob->EICPrinter->ResolPourRTF / Glob->ResolutionX);
      int nbByteForlpRgnData = ::GetRegionData( hrgn, 0, NULL);
      lpRgnData = (LPRGNDATA)::_GlobalAlloc( TAGL, GMEM_FIXED, nbByteForlpRgnData);
      int rGetRegionData = ::GetRegionData( hrgn, nbByteForlpRgnData, lpRgnData);
      ChkSDK( Tag, ((rGetRegionData==1)||(rGetRegionData==nbByteForlpRgnData)), false);
      ChkSDK( Tag, (hrgn = ::ExtCreateRegion( &Xform, nbByteForlpRgnData, lpRgnData)), NULL);
    }
    int Retour;
    TRACEF( Tag, "SelectClipRgn hdc[%X] hrgn[%X]", hdc, hrgn);
    ChkSDK( Tag, (Retour =::SelectClipRgn( hdc, hrgn)), ERROR);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    if( IsPrinting() && Glob->EICPrinter->PDF && hrgn)
    {
      ::_DeleteObject( TAGL, hrgn);
      ::_GlobalFree( TAGL, lpRgnData);
    }
    return Retour;
}

HGDIOBJ __fastcall _SelectObject( EICTag Tag, HDC hdc, HGDIOBJ hObject)
{
    HGDIOBJ Retour;
    TRACEF( Tag, "SelectObject hdc[%X] hObject[%X]", hdc, hObject);
    ChkSDK( Tag, (Retour =::SelectObject( hdc, hObject)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
void __fastcall _DeleteObject( EICTag Tag, HGDIOBJ hObject)
{
    TRACEF( Tag, "DeleteObject hObject[%X]", hObject);
    try{
    ChkSDK( Tag, ::DeleteObject( hObject), false);
    }catch(...){
        TRACEF( Tag, "Exception provoquée par DeleteObject");
    }
}
HGDIOBJ __fastcall _GetCurrentObject( EICTag Tag, HDC hdc, UINT uObjectType)
{
    HGDIOBJ Retour;
    TRACEF( Tag, "GetCurrentObject hdc[%X] uObjectType[%u]", hdc, uObjectType);
    ChkSDK( Tag, (Retour =::GetCurrentObject( hdc, uObjectType)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
int __fastcall _GetObject( EICTag Tag, HGDIOBJ hgdiobj, int cbBuffer, LPVOID lpvObject)
{
    int Retour;
    TRACEF( Tag, "GetObject hgdiobj[%X] cbBuffer[%d] lpvObject[%p]", hgdiobj, cbBuffer, lpvObject);
    ChkSDK( Tag, (Retour =::GetObject( hgdiobj, cbBuffer, lpvObject)), 0);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    return Retour;
}

HFONT __fastcall _CreateFontIndirect( EICTag Tag, CONST LOGFONT *lplf)
{
    HFONT Retour;
    TRACEF( Tag, "CreateFontIndirect lplf->lfHeight[%d] lplf->lfWidth[%d] lplf->lfEscapement[%d] lplf->lfOrientation[%d] lplf->lfWeight[%u]\n"
             "\t lplf->lfItalic[%u] lplf->lfUnderline[%u] lplf->lfStrikeOut[%u] lplf->lfCharSet[%u] lplf->lfOutPrecision[%u] lplf->lfClipPrecision[%u]\n"
             "\t lplf->lfQuality[%u] lplf->lfPitchAndFamily[%u] lplf->lfFaceName[%s]",
                                    lplf->lfHeight,    lplf->lfWidth,    lplf->lfEscapement,    lplf->lfOrientation,    lplf->lfWeight,
                lplf->lfItalic,     lplf->lfUnderline,    lplf->lfStrikeOut,    lplf->lfCharSet,    lplf->lfOutPrecision,    lplf->lfClipPrecision,
                lplf->lfQuality,     lplf->lfPitchAndFamily,    lplf->lfFaceName );

    ChkSDK( Tag, (Retour =::CreateFontIndirect( lplf)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
HBRUSH __fastcall _CreateSolidBrush( EICTag Tag, COLORREF crColor)
{
    HBRUSH Retour;
    TRACEF( Tag, "CreateSolidBrush crColor[%X]", crColor);
    ChkSDK( Tag, (Retour =::CreateSolidBrush( crColor)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
HRGN __fastcall _CreateRectRgn( EICTag Tag, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect)
{
    HRGN Retour;
    TRACEF( Tag, "CreateRectRgn nLeftRect[%d] nTopRect[%d] nRightRect[%d] nBottomRect[%d]", nLeftRect, nTopRect, nRightRect, nBottomRect);
    ChkSDK( Tag, (Retour =::CreateRectRgn( nLeftRect, nTopRect, nRightRect, nBottomRect)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;

}
HRGN __fastcall _CreateRectRgnIndirect( EICTag Tag, CONST RECT *lprc)
{
    HRGN Retour;
    ASSERT( lprc);
    TRACEF( Tag, "CreateRectRgnIndirect lprc[%d,%d,%d,%d]", lprc->left, lprc->top, lprc->right, lprc->bottom);
    ChkSDK( Tag, (Retour =::CreateRectRgnIndirect( lprc)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;

}
int __fastcall _CombineRgn( EICTag Tag, HRGN hrgnDest, HRGN hrgnSrc1, HRGN hrgnSrc2, int fnCombineMode)
{
    int Retour;
    TRACEF( Tag, "CombineRgn hrgnDest[%X] hrgnSrc1[%X] hrgnSrc2[%X] fnCombineMode[%d]", hrgnDest, hrgnSrc1, hrgnSrc2, fnCombineMode);
    ChkSDK( Tag, (Retour =::CombineRgn( hrgnDest, hrgnSrc1, hrgnSrc2, fnCombineMode)), ERROR);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    return Retour;
}
void __fastcall _SetRectRgn( EICTag Tag, HRGN hrgn, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect)
{
    TRACEF( Tag, "SetRectRgn hrgn[%X] nLeftRect[%d] nTopRect[%d] nRightRect[%d] nBottomRect[%d]", hrgn, nLeftRect, nTopRect, nRightRect, nBottomRect);
    ChkSDK( Tag, ::SetRectRgn( hrgn, nLeftRect, nTopRect, nRightRect, nBottomRect), false);
}
int     __fastcall _GetRgnBox( EICTag Tag, HRGN hrgn, LPRECT lprc)
{
    int Retour;
    TRACEF( Tag, "GetRgnBox hrgn[%X] lprc[%X] ", hrgn, lprc);
    ChkSDK( Tag, (Retour =::GetRgnBox( hrgn, lprc)), ERROR);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    return Retour;
}
bool    __fastcall _RectInRegion( EICTag Tag, HRGN hrgn, CONST RECT *lprc)
{
    bool Retour;
    TRACEF( Tag, "RectInRegion hrgn[%X] lprc->left[%d] lprc->top[%d] lprc->right[%d] lprc->bottom[%d]", hrgn, lprc->left, lprc->top, lprc->right, lprc->bottom);
    Retour = ::RectInRegion( hrgn, lprc); // pas d'erreur
    TRACEF( Tag, "\t\tRetour[%d]", (int)Retour);
    return Retour;
}
HRGN    __fastcall _CreatePolygonRgn(EICTag Tag, CONST POINT *lppt,	int cPoints, int fnPolyFillMode)
{
    HRGN Retour;
    TRACEF( Tag, "CreatePolygonRgn lppt[%p] cPoints[%d] fnPolyFillMode[%d]", lppt, cPoints, fnPolyFillMode);
    ChkSDK( Tag, (Retour = ::CreatePolygonRgn( lppt, cPoints, fnPolyFillMode)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
HBITMAP __fastcall _CreateBitmap( EICTag Tag, int nWidth, int nHeight, UINT cPlanes, UINT cBitsPerPel, CONST VOID *lpvBits)
{
    HBITMAP Retour;
    TRACEF( Tag, "CreateBitmap nWidth[%d] nHeight[%d] cPlanes[%u], cBitsPerPel[%u] lpvBits[%p]", nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
    ChkSDK( Tag, (Retour = ::CreateBitmap( nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
HBITMAP __fastcall _CreateCompatibleBitmap( EICTag Tag, HDC hdc, int nWidth, int nHeight)
{
    HBITMAP Retour;
    TRACEF( Tag, "CreateCompatibleBitmap hdc[%X] nWidth[%d] nHeight[%d]", hdc, nWidth, nHeight);
    ChkSDK( Tag, (Retour = ::CreateCompatibleBitmap( hdc, nWidth, nHeight)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}


COLORREF __fastcall _GetTextColor( EICTag Tag, HDC hdc)
{
    COLORREF Retour;
    TRACEF( Tag, "GetTextColor hdc[%X]", hdc);
    ChkSDK( Tag, (Retour =::GetTextColor( hdc)), CLR_INVALID);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
COLORREF __fastcall _SetTextColor( EICTag Tag, HDC hdc,	COLORREF crColor)
{
    COLORREF Retour;
    TRACEF( Tag, "SetTextColor hdc[%X] crColor[%X]", hdc, crColor);
    ChkSDK( Tag, (Retour =::SetTextColor( hdc, crColor)), CLR_INVALID);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
COLORREF __fastcall _SetBkColor( EICTag Tag, HDC hdc, COLORREF crColor)
{
    int i;
    COLORREF Retour;
    TRACEF( Tag, "SetBkColor hdc[%X] crColor[%X]", hdc, crColor);
    if( crColor == 0) crColor = 0xEEEEEE;
    ChkSDK( Tag, (Retour =::SetBkColor( hdc, crColor)), CLR_INVALID);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
int __fastcall _SetBkMode( EICTag Tag, HDC hdc, int iBkMode)
{
    int Retour;
    TRACEF( Tag, "SetBkMode hdc[%X] iBkMode[%d]", hdc, iBkMode);
    ChkSDK( Tag, (Retour =::SetBkMode( hdc, iBkMode)), 0);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    return Retour;
}
COLORREF __fastcall _GetNearestColor( EICTag Tag, HDC hdc, COLORREF crColor)
{
    COLORREF Retour;
    TRACEF( Tag, "GetNearestColor hdc[%X] crColor[%X]", hdc, crColor);
    ChkSDK( Tag, (Retour =::GetNearestColor( hdc, crColor)), CLR_INVALID);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}

void __fastcall _BeginPath( EICTag Tag, HDC hdc)
{
    TRACEF( Tag, "BeginPath hdc[%X]", hdc);
    ChkSDK( Tag, ::BeginPath( hdc), false);
}
void __fastcall _EndPath( EICTag Tag, HDC hdc)
{
    TRACEF( Tag, "EndPath hdc[%X]", hdc);
    ChkSDK( Tag, ::EndPath( hdc), false);
}
HRGN __fastcall _PathToRegion( EICTag Tag, HDC hdc)
{
    HRGN Retour;
    TRACEF( Tag, "PathToRegion hdc[%X]", hdc);
    ChkSDK( Tag, (Retour =::PathToRegion( hdc)), ERROR);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    return Retour;
}
void __fastcall _StrokePath( EICTag Tag, HDC hdc)
{
    TRACEF( Tag, "StrokePath hdc[%X]", hdc);
    ChkSDK( Tag, ::StrokePath( hdc), false);
}
int  __fastcall _OffsetRgn( EICTag Tag, HRGN hrgn, int nXOffset, int nYOffset)
{
    int Retour;
    TRACEF( Tag, "OffsetRgn hrgn[%X] nXOffset[%d] nYOffset[%d]", hrgn, nXOffset, nYOffset);
    ChkSDK( Tag, (Retour = ::OffsetRgn( hrgn, nXOffset, nYOffset)), 0);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    return Retour;
}



int __fastcall _SetMapMode( EICTag Tag, HDC hdc, int fnMapMode)
{
    int Retour;
    TRACEF( Tag, "SetMapMode hdc[%X] fnMapMode[%d]", hdc, fnMapMode);
    ChkSDK( Tag, (Retour = ::SetMapMode( hdc, fnMapMode)), ERROR);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    return Retour;
}
int     __fastcall _GetMapMode( EICTag Tag, HDC hdc)
{
    int Retour;
    TRACEF( Tag, "GetMapMode hdc[%X] ", hdc);
    ChkSDK( Tag, (Retour = ::GetMapMode( hdc)), 0);
    TRACEF( Tag, "\t\tRetour[%d]", Retour);
    return Retour;
}

void __fastcall _SetWindowExtEx( EICTag Tag, HDC hdc, int nXExtent, int nYExtent, LPSIZE lpSize)
{
    TRACEF( Tag, "SetWindowExtEx hdc[%X] nXExtent[%d] nYExtent[%d] lpSize[%p]", hdc, nXExtent, nYExtent, lpSize);
    ChkSDK( Tag, (::SetWindowExtEx( hdc, nXExtent, nYExtent, lpSize)), false);
}
void    __fastcall _SetViewportExtEx( EICTag Tag, HDC hdc, int nXExtent, int nYExtent, LPSIZE lpSize)
{
    TRACEF( Tag, "SetViewportExtEx hdc[%X] nXExtent[%d] nYExtent[%d] lpSize[%p]", hdc, nXExtent, nYExtent, lpSize);
    ChkSDK( Tag, (::SetViewportExtEx( hdc, nXExtent, nYExtent, lpSize)), false);
}



HLOCAL __fastcall _LocalAlloc( EICTag Tag, UINT uFlags, UINT uBytes)
{
    HLOCAL Retour;
    TRACEF( Tag, "LocalAlloc uFlags[%u] uBytes[%X]", uFlags, uBytes);
    ChkSDK( Tag, (Retour = ::LocalAlloc( uFlags, uBytes)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
void __fastcall _LocalFree( EICTag Tag, HLOCAL hMem)
{
    HLOCAL Retour;
    TRACEF( Tag, "LocalFree hMem[%X]", hMem);
    ChkSDK( Tag, ::LocalFree( hMem), hMem);
}
HGLOBAL __fastcall _GlobalAlloc( EICTag Tag, UINT uFlags, UINT uBytes)
{
    HGLOBAL Retour;
    TRACEF( Tag, "GlobalAlloc uFlags[%u] uBytes[%X]", uFlags, uBytes);
    ChkSDK( Tag, (Retour = ::GlobalAlloc( uFlags, uBytes)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
LPVOID  __fastcall _GlobalLock( EICTag Tag, HGLOBAL hMem)
{
    LPVOID Retour;
    TRACEF( Tag, "GlobalLock hMem[%X] ", hMem);
    ChkSDK( Tag, (Retour = ::GlobalLock( hMem)), NULL);
    TRACEF( Tag, "\t\tRetour[%p]", Retour);
    return Retour;
}
void __fastcall _GlobalUnlock( EICTag Tag, HGLOBAL hMem)
{
    TRACEF( Tag, "GlobalUnlock hMem[%X]", hMem);
    ::SetLastError(NO_ERROR);
    bool b = ::GlobalUnlock( hMem);
    int nLastError = ::GetLastError();
    ChkSDK( Tag, (b || ( nLastError == NO_ERROR)), false);
}
void __fastcall _GlobalFree( EICTag Tag, HGLOBAL hMem)
{
    TRACEF( Tag, "GlobalFree hMem[%X]", hMem);
    ChkSDK( Tag, ::GlobalFree( hMem), hMem);
}

int __fastcall _StartDoc( EICTag Tag, HDC hdc, CONST DOCINFO *lpdi)
{
    int Retour;
    TRACEF( Tag, "StartDoc hdc[%X] lpdi->lpszDocName[%s] lpdi->lpszOutput[%s] lpdi->lpszDatatype[%s] lpdi->fwType[%d]",
                hdc, lpdi->lpszDocName,
                lpdi->lpszOutput?lpdi->lpszOutput:"(null)",
                lpdi->lpszDatatype?lpdi->lpszDatatype:"(null)",
                lpdi->fwType);
    Retour = ::StartDoc( hdc, lpdi);
    if( Retour<=0) {AfficheLastErrorT( "StartDoc");}
    return Retour;
}
int __fastcall _StartPage( EICTag Tag, HDC hdc)
{
    int Retour;
    TRACEF( Tag, "StartPage hdc[%X]", hdc);
    Retour = ::StartPage( hdc);
    if( Retour <= 0) {AfficheLastErrorT( "StartPage");}
    return Retour;
}
int __fastcall _EndPage( EICTag Tag, HDC hdc)
{
    int Retour;
    TRACEF( Tag, "EndPage hdc[%X]", hdc);
    Retour = ::EndPage( hdc);
    if( Retour<=0) {AfficheLastErrorT( "EndPage");}
    return Retour;
}
int __fastcall _EndDoc( EICTag Tag, HDC hdc)
{
    int Retour;
    TRACEF( Tag, "EndDoc hdc[%X]", hdc);
    Retour = ::EndDoc( hdc);
    if( Retour<=0) {AfficheLastErrorT( "EndDoc");}
    return Retour;
}
int __fastcall _AbortDoc( EICTag Tag, HDC hdc)
{
    int Retour;
    TRACEF( Tag, "AbortDoc hdc[%X]", hdc);
    ChkSDK( Tag, (Retour = ::AbortDoc( hdc)), SP_ERROR);
    return Retour;
}

void    __fastcall _OpenPrinter( EICTag Tag, LPTSTR pPrinterName,	LPHANDLE phPrinter,	LPPRINTER_DEFAULTS pDefault)
{
    int Retour;
    TRACEF( Tag, "OpenPrinter pPrinterName[%s] phPrinter[%p] pDefault[%p]", pPrinterName, phPrinter, pDefault);
    ChkSDK( Tag, ::OpenPrinter( pPrinterName, phPrinter, pDefault), 0);
}

void    __fastcall _ClosePrinter( EICTag Tag, HANDLE hPrinter)
{
    int Retour;
    TRACEF( Tag, "ClosePrinter hdc[%X]", hPrinter);
    ChkSDK( Tag, ::ClosePrinter( hPrinter), 0);
}

HDC     __fastcall _ResetDC( EICTag Tag, HDC hdc,	CONST DEVMODE *lpInitData)
{
    HDC Retour;
    TRACEF( Tag, "ResetDC hdc[%X] lpInitData[%p]", hdc, lpInitData);
    ChkSDK( Tag, (Retour = ::ResetDC( hdc, lpInitData)), NULL);
    return Retour;
}

LONG    __fastcall _DocumentProperties( EICTag Tag, HWND hWnd, HANDLE hPrinter,	LPTSTR pDeviceName,	PDEVMODE pDevModeOutput, PDEVMODE pDevModeInput, DWORD fMode)
{
    LONG Retour;
    TRACEF( Tag, "DocumentProperties hWnd[%X] hPrinter[%X] pDeviceName[%s] pDevModeOutput[%p] pDevModeInput[%p], fMode[%X]",
                hWnd, hPrinter, pDeviceName, pDevModeOutput, pDevModeInput, fMode);
    ChkSDK_2( TAGL, ( Retour = ::DocumentProperties( hWnd, hPrinter, pDeviceName, pDevModeOutput, pDevModeInput, fMode)), < 0);
    return Retour;
}
UINT __fastcall _GetMetaFileBitsEx( EICTag Tag, HMETAFILE hmf, UINT nSize, LPVOID lpvData)
{
    UINT Retour;
    TRACEF( Tag, "GetMetaFileBitsEx hmf[%X] nSize[%d], lpvData[%p]", hmf, nSize, lpvData);
    ChkSDK( Tag, (Retour = ::GetMetaFileBitsEx( hmf, nSize, lpvData)), NULL);
    TRACEF( Tag, "\t\tRetour[%u]", Retour);
    return Retour;
}
HENHMETAFILE __fastcall _SetWinMetaFileBits( EICTag Tag, UINT cbBuffer, CONST BYTE *lpbBuffer, HDC hdcRef, CONST METAFILEPICT *lpmfp)
{
    HENHMETAFILE Retour;
    TRACEF( Tag, "SetWinMetaFileBits cbBuffer[%d] lpbBuffer[%p], hdcRef[%X] lpmfp[%p]", cbBuffer, lpbBuffer, hdcRef, lpmfp);
    ChkSDK( Tag, (Retour = ::SetWinMetaFileBits( cbBuffer, lpbBuffer, hdcRef, lpmfp)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
void __fastcall _DeleteMetaFile( EICTag Tag, HMETAFILE hemf)
{
    TRACEF( Tag, "DeleteMetaFile hemf[%X]", hemf);
    ChkSDK( Tag, ::DeleteMetaFile( hemf), false);
}

HDC __fastcall _CreateEnhMetaFile( EICTag Tag, HDC hdcRef, LPCTSTR lpFilename, CONST RECT *lpRect, LPCTSTR lpDescription)
{
    HDC Retour;
    TRACEF( Tag, "CreateEnhMetaFile hdc[%X] lpFilename[%s] lpRect[%d,%d,%d,%d] lpDescription[%s]",
                 hdcRef, lpFilename?lpFilename:"(null)", lpRect->left, lpRect->top, lpRect->right, lpRect->bottom, lpDescription?lpDescription:"(null)");
    Retour = ::CreateEnhMetaFile( hdcRef, lpFilename, lpRect, lpDescription);
    if( Retour==NULL) {AfficheLastErrorT( "CreateEnhMetaFile");}
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
HENHMETAFILE __fastcall _GetEnhMetaFile( EICTag Tag, LPCTSTR lpszMetaFile)
{
    HENHMETAFILE Retour;
    TRACEF( Tag, "GetEnhMetaFile lpszMetaFile[%s]", lpszMetaFile);
    ChkSDK( Tag, (Retour = ::GetEnhMetaFile( lpszMetaFile)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}

HENHMETAFILE __fastcall _CloseEnhMetaFile( EICTag Tag, HDC hdc)
{
    HENHMETAFILE Retour;
    TRACEF( Tag, "CloseEnhMetaFile hdc[%X]", hdc);
    ChkSDK( Tag, (Retour = ::CloseEnhMetaFile( hdc)), NULL);
    TRACEF( Tag, "\t\tRetour[%X]", Retour);
    return Retour;
}
void __fastcall _PlayEnhMetaFile( EICTag Tag, HDC hdc, HENHMETAFILE hemf, CONST RECT *lpRect)
{
    TRACEF( Tag, "PlayEnhMetaFile hdc[%X] hemf[%X] lpRect[%d,%d,%d,%d]",
                    hdc, hemf, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
    ChkSDK( Tag, (::PlayEnhMetaFile( hdc, hemf, lpRect)), false);
}
void  __fastcall _DeleteEnhMetaFile( EICTag Tag, HENHMETAFILE hemf)
{
    TRACEF( Tag, "DeleteEnhMetaFile hemf[%X]", hemf);
    ChkSDK( Tag, (::DeleteEnhMetaFile( hemf)), false);
}
UINT         __fastcall _GetEnhMetaFileHeader( EICTag Tag, HENHMETAFILE hemf,	UINT cbBuffer, LPENHMETAHEADER lpemh)
{
    UINT Retour;    
    TRACEF( Tag, "GetEnhMetaFileHeader hemf[%X] cbBuffer[%d], lpemh[%p]", hemf, cbBuffer, lpemh);
    ChkSDK( Tag, (Retour = ::GetEnhMetaFileHeader( hemf, cbBuffer, lpemh)), NULL);
    TRACEF( Tag, "\t\tRetour[%u]", Retour);
    return Retour;
}

HDC __fastcall _MyCreateEnhMetaFile( EICTag Tag, const TCoordonnee &X, const TCoordonnee &Y,
                                    DWORD dwDPI, const AnsiString &s )       // DPI (logical units)
{
    RECT   Rect = { 0, 0, 0, 0 };
    HDC      hMetaDC;//, hScreenDC;

    // dwInchesX x dwInchesY in .01mm units
    SetRect( &Rect, 0, 0, X.Mm*100, Y.Mm*100 );

    // Create the Metafile
    hMetaDC = ::_CreateEnhMetaFile(TAGL, Glob->hDCEcran, s.IsEmpty()?NULL:s.c_str(), &Rect, NULL);
    // Did you get a good metafile?
    if( hMetaDC == NULL )
        return NULL;
    if( hMetaDC && dwDPI)
    {
        // Anisotropic mapping mode
        ::_SetMapMode( TAGL, hMetaDC, MM_ANISOTROPIC );

        // Set the Windows extent
        ::_SetWindowExtEx( TAGL, hMetaDC, X.Pouces*dwDPI, Y.Pouces*dwDPI, NULL );

        // Get a Reference DC
        InitDCEcran();

        float   PixelsX, PixelsY, MMX, MMY;
        // Get the physical characteristics of the reference DC
        PixelsX = (float)::_GetDeviceCaps( TAGL, Glob->hDCEcran, HORZRES );
        PixelsY = (float)::_GetDeviceCaps( TAGL, Glob->hDCEcran, VERTRES );
        MMX = (float)::_GetDeviceCaps( TAGL, Glob->hDCEcran, HORZSIZE );
        MMY = (float)::_GetDeviceCaps( TAGL, Glob->hDCEcran, VERTSIZE );
        // Set the viewport extent to reflect
        // dwInchesX" x dwInchesY" in device units
        ::_SetViewportExtEx( TAGL, hMetaDC,
                            (int)((float)X.Pouces*MM_PER_INCH*PixelsX/MMX),
                            (int)((float)Y.Pouces*MM_PER_INCH*PixelsY/MMY),
                            NULL );

    }
    return hMetaDC;
}

